using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpeedFoodCon : MonoBehaviour
{
    public BoxCollider2D _gridAera;
    private float x;
    private float y;
    private Vector2 _spawnPosition;
    private Collider2D _hitBodyCollider;
    public LayerMask _snakeBodyLayer;
    public GameObject _snakeObject;
    public void CreateSpeedUpFood() 
    {
        Bounds bounds = _gridAera.bounds;
        do
        {
            x = Random.Range(bounds.min.x, bounds.max.x);
            y = Random.Range(bounds.min.y, bounds.max.y);
            _spawnPosition = new Vector2(x, y);
            //检测速度食物生成位置是否是蛇身体位置
            _hitBodyCollider = Physics2D.OverlapCircle(_spawnPosition, 0.1f, _snakeBodyLayer);
        }
        while (_hitBodyCollider != null);
        this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            _snakeObject.GetComponent<SnakeMove>().ApplySpeedBoost(0.1f, 5f);
        }
        else 
        {
            CreateSpeedUpFood();
        }
        this.gameObject.SetActive(false);
    }
}
